package com.pj.wisdomlogistics.ui.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.pj.wisdomlogistics.MyApplication;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.MyApplySimpleInfo;
import com.pj.wisdomlogistics.mvp.contract.MyTransactContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.MyTransactPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.ListDropDownAdapter;
import com.pj.wisdomlogistics.ui.adapter.MyApplySimpleAdapter;
import com.pj.wisdomlogistics.ui.view.recyclerview.LoadMoreRecyclerView;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;
import com.yyydjk.library.DropDownMenu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyTransactActivity extends MvpActivity<MyTransactPresenter> implements MyTransactContract.View, MyApplySimpleAdapter.onItemClick, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.dropdownmenu)
    DropDownMenu mDropDownMenu;


    private List<String> mTimeList = new ArrayList<>();
    private List<String> mStateList = new ArrayList<>();
    private List<String> mContentList = new ArrayList<>();

    private MyApplySimpleAdapter mAdapter;

    private List<View> popupViews = new ArrayList<>();
    private String headers[] =  new String[]{ "全部", "全部"};

    private ListDropDownAdapter mTimeAdapter;
    private ListDropDownAdapter mStateAdapter;
    private ListDropDownAdapter mContentAdapter;

    private int currentTime;
    private int currentState = 1;
    private int currentContent = 1;

    private LoadMoreRecyclerView mRvTransact;
    private SwipeRefreshLayout mRefreshLayout;

    private int pageIndex = 1;
    private int pageCount = 10;

    private int totalCount;
    private int totalPage;
    private boolean isFirst = true;

    private LinearLayout mLayout;
    private LinearLayout mLlImage;//无数据情况

    @Override
    public void initView() {
        mTimeList = Arrays.asList(new String[]{"全部", "今天"});
        mContentList = Arrays.asList(new String[]{"全部" ,"公务用车", "会议室" , "维修" , "用餐"});

        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.text_blue));

        mLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_refresh_rv, null);
        mRvTransact = (LoadMoreRecyclerView) mLayout.findViewById(R.id.recyclerview);
        mRefreshLayout = (SwipeRefreshLayout) mLayout.findViewById(R.id.refresh);
        mLlImage = (LinearLayout) mLayout.findViewById(R.id.ll_noData);

        mRvTransact.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyApplySimpleAdapter(this);
        mRvTransact.setAdapter(mAdapter);
        mAdapter.setListener(this);

        mRvTransact.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                mPresenter.myTodo(MyApplication.userID, currentContent , currentTime, currentState, ++pageIndex, pageCount);
                mRvTransact.notifyMoreFinish(pageIndex < totalPage);
            }
        });


        mRefreshLayout.setColorSchemeResources(
                R.color.google_orange,
                R.color.google_yellow,
                R.color.google_blue,
                R.color.google_green
        );

        mRefreshLayout.setOnRefreshListener(this);

        ListView contentView = new ListView(this);
        contentView.setDividerHeight(0);
        mContentAdapter = new ListDropDownAdapter(this);
        mContentAdapter.setData(mContentList);
        contentView.setAdapter(mContentAdapter);

        ListView timeView = new ListView(this);
        timeView.setDividerHeight(0);
        mTimeAdapter = new ListDropDownAdapter(this);
        mTimeAdapter.setData(mTimeList);
        timeView.setAdapter(mTimeAdapter);

//        ListView stateView = new ListView(this);
//        stateView.setDividerHeight(0);
//        mStateAdapter = new ListDropDownAdapter(this);
//        mStateAdapter.setData(mStateList);
//        stateView.setAdapter(mStateAdapter);


        popupViews.add(contentView);
        popupViews.add(timeView);
       // popupViews.add(stateView);


        contentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentContent = position + 1;

                mContentAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[0] : mContentList.get(position), 0);
                mDropDownMenu.closeMenu();

                toRefresh();
            }
        });

        timeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentTime = position;

                mTimeAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[1] : mTimeList.get(position), 2);
                mDropDownMenu.closeMenu();

                toRefresh();
            }
        });

//        stateView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                currentState = position;
//
//                mStateAdapter.setCheckItem(position + 1);
//                mDropDownMenu.setTabText(position == 0 ? headers[2] : mStateList.get(position), 4);
//                mDropDownMenu.closeMenu();
//
//                toRefresh();
//            }
//        });


        mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, mLayout);

        mPresenter.myTodo(MyApplication.userID, currentContent , currentTime, currentState, pageIndex, pageCount);
        showProgressDialog();
    }

    @Override
    public void initData() {

    }


    @Override
    public int getContentView() {
        return R.layout.activity_my_transact;
    }

    @Override
    public boolean isApplyEventBus() {
        return true;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {
        if(eventCenter.getEventCode() == Cons.Other.MY_TRANSACT){
            toRefresh();
        }
    }

    @OnClick({R.id.iv_back , R.id.tv_history})
    public void onClick(View mView) {
        switch (mView.getId()){
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_history:
                startActivity(TransactHistoryActivity.class);
                break;
        }
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }


    @Override
    public void itemClick(int type, int id) {
        Intent mIntent = null;
        switch (type) {
            case Cons.ApplyType.APPLY_GWYC:
                mIntent = new Intent(this, CarDetailActivity.class);
                mIntent.putExtra(Cons.CarInfo.CAR_TRANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_HYS:
                mIntent = new Intent(this, ApplyDetailActivity.class);
                mIntent.putExtra(Cons.TransactDetail.MEETING_TRANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_ST:
                mIntent = new Intent(this , MealDetialActivity.class);
                mIntent.putExtra(Cons.MealInfo.MEAL_RANSACT_ID , id);
                break;
            case Cons.ApplyType.APPLY_WHBX:
                mIntent = new Intent(this, RepairDetailActivity.class);
                mIntent.putExtra(Cons.RepairInfo.REPAIR_RANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_WMDC:
                break;
        }
        mIntent.putExtra(Cons.ApplyDetail.MEETING_ID, id);
        startActivity(mIntent);
    }

    @Override
    public void onRefresh() {
        toRefresh();
    }

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this ,errorMsg , 1000);
    }

    @Override
    public void showSuccess(MyApplySimpleInfo mInfo) {
        dismissProgressDialog();

        if (mRefreshLayout.isRefreshing()) {
            //下拉刷新
            if(mInfo == null || mInfo.getList() == null){
                mAdapter.removeAll();
            }else{
                mAdapter.setList(mInfo.getList());
            }
            mRefreshLayout.setRefreshing(false);
        }
        //无数据情况展示
        if ((mInfo == null || mInfo.getList() == null) && pageIndex == 1) {
            mAdapter.removeAll();
            mLlImage.setVisibility(View.VISIBLE);
            mRvTransact.setVisibility(View.GONE);
            return;
        } else {
            mLlImage.setVisibility(View.GONE);
            mRvTransact.setVisibility(View.VISIBLE);
        }
        if (isFirst) {
            if( mInfo == null || mInfo.getList().size() == 0){
                mAdapter.removeAll();
                return;
            }
            //计算是否需要footview
            totalCount = mInfo.getCount();
            if (pageCount < totalCount) {
                totalPage = totalCount % pageCount == 0 ? totalCount / pageCount : totalCount / pageCount + 1;
            } else {
                totalPage = 1;
            }
            mRvTransact.setAutoLoadMoreEnable(pageIndex < totalPage);
            isFirst = false;

            mAdapter.setList(mInfo.getList());
        } else {
            //上拉加载
            mAdapter.appendList(mInfo.getList());
        }


    }

    //下拉框重新选择后，刷新
    private void toRefresh() {
        pageIndex = 1;
        isFirst = true;
        mPresenter.myTodo(MyApplication.userID, currentContent ,currentTime, currentState, pageIndex, pageCount);
    }

}
